package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.PageResult;
import com.woniu.mapper.PrivateTimetableMapper;
import com.woniu.pojo.PrivateCourses;
import com.woniu.mapper.PrivateCoursesMapper;
import com.woniu.pojo.PrivateTimetable;
import com.woniu.service.IPrivateCoursesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.service.IPrivateTimetableService;
import com.woniu.vo.PrivateCourseQueryVo;
import com.woniu.vo.PrivateCoursesVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaohuang
 * @since 2022-10-12
 */
@Service
public class PrivateCoursesServiceImpl extends ServiceImpl<PrivateCoursesMapper, PrivateCourses> implements IPrivateCoursesService {
    @Autowired
    private PrivateCoursesMapper privateCoursesMapper;
    @Autowired
    private PrivateTimetableMapper privateTimetableMapper;
    @Autowired
    private IPrivateTimetableService privateTimetableService;

    @Override
    public PageResult search(PrivateCourseQueryVo courseQueryVo, Integer currentPage, Integer pageSize) {

        Page page = new Page(currentPage,pageSize);
        //关闭mybatis-plus对sql的自动优化（在count查询时舍弃掉join的内容）
        //解决方案一，把optimizeCountSql指定为false，
        //page.setOptimizeCountSql(false);
        //方案二，手写count sql
        page.setSearchCount(false);//关闭count sql
        IPage coursesIPage = privateCoursesMapper.findPage(page, courseQueryVo);
        //调用手写count sql
        coursesIPage.setTotal(privateCoursesMapper.repListCount(courseQueryVo));
        PageResult pageResult = new PageResult(coursesIPage.getTotal(), coursesIPage.getRecords());
        System.out.println("私课分页查询总数：" + coursesIPage.getTotal());
        System.out.println("总数：" + privateCoursesMapper.repListCount(courseQueryVo));
        return pageResult;
    }

    @Override
    public Integer deleteById(String id) {
        QueryWrapper<PrivateTimetable> wrapper = new QueryWrapper<>();
        wrapper.eq("private_course_id", id);

        List<PrivateTimetable> timetables = privateTimetableMapper.selectList(wrapper);
        if (timetables != null){
            for (PrivateTimetable timetable : timetables) {

                String timetableId = timetable.getId();
                System.out.println("删除的课表id：" + timetableId);
                Integer integer = privateTimetableService.deleteById(timetableId);
                System.out.println("是否删除课表：" +integer);
            }
        }
        Integer i = privateCoursesMapper.deleteById(id);
        return i;
    }


}
